package mdt.gui;

import net.miginfocom.swing.MigLayout;
import org.tigris.gef.presentation.Fig;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import org.tigris.gef.presentation.Fig.FILL_TYPE;

/**
 * Created by xiaobyu on 7/18/2016.
 */
public class ShapeAttributePanel extends AttributePanel {

    JButton lineColorButton;
    JButton fillColorButton;
    JScrollBar lineWidthBar;
    //JComboBox lineStyleBox;
    JCheckBox dashed;

    JRadioButton noFill;
    JRadioButton fill;
    JComboBox fillColorTypeCombo;
    JButton gradientStart;
    JButton gradientEnd;


    static final FILL_TYPE[] fillColorType = new FILL_TYPE[]{
            FILL_TYPE.SOLID,
            FILL_TYPE.LINEAR_GRADIENT,
            FILL_TYPE.LINEAR_REFLECT_GRADIENT,
            FILL_TYPE.RADIAL_GRADIENT
    };

    public ShapeAttributePanel() {
        initFillColor();
        initLineColor();
        initLineStyle();
        setPreferredSize(new Dimension(350, 300));
    }

    protected void initLayout() {
        setLayout(new MigLayout("", "[]15[grow,fill,40]20[]15[grow,fill,40]", ""));
    }
    JLabel fillColorLabel;
    JLabel startColorLabel;
    JLabel endColorLabel;

    private void initFillColor() {
        addSeparator("Fill Color");

        ButtonGroup bg = new ButtonGroup();
        noFill = new JRadioButton();
        noFill.setAction(new AbstractAction("No Fill") {
            @Override
            public void actionPerformed(ActionEvent e) {
                setFilled(false);
                fillColorLabel.setVisible(false);
                fillColorButton.setVisible(false);
                startColorLabel.setVisible(false);
                gradientStart.setVisible(false);
                endColorLabel.setVisible(false);
                gradientEnd.setVisible(false);
                fillColorTypeCombo.setEnabled(false);
            }
        });
        fill = new JRadioButton();
        fill.setAction(new AbstractAction("Fill type") {
            @Override
            public void actionPerformed(ActionEvent e) {
                setFilled(true);
                fillColorTypeCombo.setEnabled(true);
                setFillColorType(firstFig.getFillType());
            }
        });
        bg.add(noFill);
        bg.add(fill);

        add(noFill,  "gap para, span 4, wrap");
        add(fill,  "gap para, span 4, split 2");

        fillColorTypeCombo = new JComboBox(fillColorType);
        add(fillColorTypeCombo, "grow, wrap");

        FILL_TYPE fillType = firstFig.getFillType();
        fillColorTypeCombo.setSelectedItem(fillType);

        fillColorTypeCombo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setFillColorType((FILL_TYPE) fillColorTypeCombo.getSelectedItem());
            }
        });

        fillColorLabel = new JLabel("Fill color");
        add(fillColorLabel, "hidemode 3, gap para, w 60!, span 4, split 2");
        fillColorButton = new JButton();
        fillColorButton.setBackground(firstFig.getFillColor());
        fillColorButton.setAction(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Color color = JColorChooser.showDialog(ShapeAttributePanel.this,"Select a color", firstFig.getFillColor());
                if (color != null) {
                    setFillColor(color);
                    fillColorButton.setBackground(color);
                }
            }
        });
        add(fillColorButton, "hidemode 3, h 25!, grow, wrap");

        startColorLabel = new JLabel("Start color");
        add(startColorLabel, "hidemode 3, gap para");
        gradientStart = new JButton();
        gradientStart.setBackground(firstFig.getGradientStart());

        gradientStart.setAction(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Color color = JColorChooser.showDialog(ShapeAttributePanel.this,"Select a color", firstFig.getGradientStart());
                if (color != null) {
                    setGradientStart(color);
                    gradientStart.setBackground(color);
                }
            }
        });
        add(gradientStart, "hidemode 3, h 25!, w 60::, grow");

        endColorLabel = new JLabel("End color");
        add(endColorLabel, "hidemode 3");
        gradientEnd = new JButton();
        gradientEnd.setBackground(firstFig.getGradientEnd());

        gradientEnd.setAction(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Color color = JColorChooser.showDialog(ShapeAttributePanel.this,"Select a color", firstFig.getGradientEnd());
                if (color != null) {
                    setGradientEnd(color);
                    gradientEnd.setBackground(color);
                }
            }
        });
        add(gradientEnd, "hidemode 3, h 25!, w 60::, grow, wrap");

        fillColorTypeCombo.setSelectedItem(fillType);

        if (firstFig.isFilled()) {
            fill.setSelected(true);
        } else {
            noFill.setSelected(true);
        }
    }

    private void setFillColorType(FILL_TYPE fillType) {
        for (Fig fig : figs) {
            fig.setFillType(fillType);
        }

        switch (fillType) {
            case SOLID:
                fillColorLabel.setVisible(true);
                fillColorButton.setVisible(true);
                startColorLabel.setVisible(false);
                gradientStart.setVisible(false);
                endColorLabel.setVisible(false);
                gradientEnd.setVisible(false);
                break;
            case LINEAR_GRADIENT:
            case LINEAR_REFLECT_GRADIENT:
            case RADIAL_GRADIENT:
                fillColorLabel.setVisible(false);
                fillColorButton.setVisible(false);
                startColorLabel.setVisible(true);
                gradientStart.setVisible(true);
                endColorLabel.setVisible(true);
                gradientEnd.setVisible(true);
                break;
        }
    }

    private void initLineColor() {
        addSeparator("Line Color");
        add(new JLabel("Line Color"), "gap para, w 60!, span 4, split 2");

        lineColorButton = new JButton();
        lineColorButton.setBackground(firstFig.getLineColor());
        lineColorButton.setMargin(new Insets(0, 0, 0, 0));
        lineColorButton.setAction(new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Color color = JColorChooser.showDialog(ShapeAttributePanel.this,"Select a color", firstFig.getLineColor());
                if (color != null) {
                    setLineColor(color);
                    lineColorButton.setBackground(color);
                }
            }
        });
        add(lineColorButton, "h 25!,grow, wrap");
    }

    private void initLineStyle() {
        addSeparator("Line Style");
        dashed = new JCheckBox("Use dashed line");
        dashed.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setLineDashed(dashed.isSelected());
            }
        });
        dashed.setSelected(firstFig.getDashed());
        add(dashed, "span 4, gap para,wrap");

        add(new JLabel("Line Width"), "gap para, span 4, split 3");
        final JLabel widthValueLabel = new JLabel("(" + firstFig.getLineWidth() + ")");

        add(widthValueLabel);

        lineWidthBar = new JScrollBar(JScrollBar.HORIZONTAL, firstFig.getLineWidth(), 1, 0, 20);
        lineWidthBar.addAdjustmentListener(
                new AdjustmentListener() {
                    @Override
                    public void adjustmentValueChanged(AdjustmentEvent e) {
                        setLineWidth(e.getValue());
                        widthValueLabel.setText("(" + e.getValue() + ")");
                    }
                }
        );
        add(lineWidthBar, "grow, wrap");

    }

    public void setFilled(boolean filled) {
        for (Fig fig : figs) {
            fig.setFilled(filled);
            fig.damage();
        }
    }

    public void setFillColor(Color fillColor) {
        for (Fig fig : figs) {
            fig.setFillColor(fillColor);
            fig.damage();
        }
    }

    public void setGradientStart(Color color) {
        for (Fig fig : figs) {
            fig.setGradientStart(color);
            fig.damage();
        }
    }

    public void setGradientEnd(Color color) {
        for (Fig fig : figs) {
            fig.setGradientEnd(color);
            fig.damage();
        }
    }

    public void setLineColor(Color lineColor) {
        for (Fig fig : figs) {
            fig.setLineColor(lineColor);
            fig.damage();
        }
    }

    public void setLineWidth(int lineWidthBar) {
        for (Fig fig : figs) {
            fig.setLineWidth(lineWidthBar);
            fig.damage();
        }
    }

    public void setLineDashed(boolean isDashed) {
        for (Fig fig : figs) {
            fig.setDashed(isDashed);
            fig.damage();
        }

    }

    //private void set



    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new ShapeAttributePanel());
        frame.pack();
        frame.setVisible(true);
    }
}
